import { defineStore } from 'pinia'
import { reqBatchCheckAllCart, reqcartList, reqCheckOneCart, reqdeleteOneCart, ShopListData } from '../api/shopcart/cart'

export interface stateData {
    shopCartList:ShopListData
}
export const useShopCartStore = defineStore('shopcart', {
    state():stateData{
        return{
            shopCartList:[]
        }
    },
    actions: {
        // 获取购物车列表
        async asyncShopCartList() {
            try {
                const res = await reqcartList()
                console.log(res);
                this.shopCartList = res.data[0].cartInfoList
                // console.log(this.shopCartList);
            } catch (error) {
                console.log(error);
            }
        },
        // 更改选中状态
        async asyncCheckOne(skuID:number,isChecked:number){
            try {
               await reqCheckOneCart(skuID,isChecked);     
            } catch (error) {
                return Promise.reject(error)
            }
        },

        // 全选
        async asyncBatchCheckAllCart (isChecked:number,skuIdList:any){
            try {
                await reqBatchCheckAllCart(isChecked,skuIdList)
            } catch (error) {
                return Promise.reject(error)
            }
        },
        // 删除
        async asyncDeleteOneCart(skuId:number){
            try {
                await reqdeleteOneCart(skuId)
            } catch (error) {
                return Promise.reject(error)
            }
        }
    },
    getters: {

    }
})